Here's a more thorough description of the path through the code:
public static void checkValence(Molecule mol) throws ValenceException {
...
final String PENTACOORD_N = "[$(*=,#[NX3]=,#*),$(*:n(=,#*):*),$(*=,#N#*)]";
try {
final Molecule pentacoordN = MolImporter.importMol(PENTACOORD_N); // line 1283
if (MolFunctions.containsSubstruct(mol, pentacoordN)) {
throw new ValenceException(VALENCE_ERROR);
} // if molecule contains pentavalent N
} catch (MolFormatException e) { // unlikely
; // do nothing
} catch (MolFileException e) { // unlikely
; // do nothing
} // try
} // checkValence(Molecule)
public static boolean containsSubstruct(Molecule respMol,
Molecule substruct, int chgRadIso) throws MolFileException {
...
try {
match = search.isMatching();
debugPrint(SELF + "response ", respMol, (match // line 885
? " contains" : " does not contain"),
" the substructure, ", substruct);
} catch (SearchException e2) {
Utils.alwaysPrint("Error in " + SELF);
e2.printStackTrace();
throw new MolFileException(ERROR + SELF + e2.getMessage());
}
} // containsSubstruct(Molecule, Molecule, int)
private static void printObject(Object obj, String format) {
...
} else if (obj instanceof Molecule) {
try {
System.out.print(ChemUtils.toString((Molecule) obj, format)); // line 130; format = "SMILES"
} catch (IllegalArgumentException e1) {
try {
System.out.print(ChemUtils.toString((Molecule) obj, SMARTS));
} catch (IllegalArgumentException e2) {
System.out.print(ChemUtils.toString((Molecule) obj, MRV));
} // try
} // try
} else if (obj instanceof MDocument) {
...
} // printObject(Object, String)
public static String toString(Molecule mol, String format) {
try {
return MolExporter.exportToFormat(mol, format);
} catch (IOException e) { ; }
return null;
} // toString(Molecule, String)
The exception seems to occur when I try to export [$(*=,#[NX3]=,#*),$(*:n(=,#*):*),$(*=,#N#*)] into SMILES format.
I changed printObject() to catch an Exception instead of an IllegalArgumentException. I no longer get the ClassCastException, and the log now says:
MolFunctions.containsSubstruct: response [OH2+]C1CCCCO1 does not contain the substructure, null
So JChem is still unable to export [$(*=,#[NX3]=,#*),$(*:n(=,#*):*),$(*=,#N#*)], but at least it now just returns a null value instead of throwing an exception.